package com.zzw.controller;

import com.zzw.entity.*;
import com.zzw.page.Page;
import com.zzw.service.InStoreService;
import com.zzw.service.PurchaseService;
import com.zzw.service.StoreService;
import com.zzw.utils.TokenUtils;
import com.zzw.utils.WarehouseConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Project: warehouse
 * @Description:
 * @Author: zzw
 */
@RestController
@RequestMapping("/purchase")
@Slf4j
@Api(tags = "采购模块")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private InStoreService inStoreService;

    /**
     * 查询所有仓库的url接口
     */
    @GetMapping("/store-list")
    public Result storeList() {
        //执行业务
        List<Store> storeList = storeService.queryAllStore();
        //响应
        return Result.ok(storeList);
    }

    /**
     * 添加采购单的url接口
     *
     * @param purchase
     * @return
     */
    @PostMapping
    @ApiOperation("添加采购单")
    public Result savePurchase(@RequestBody Purchase purchase) {
        return purchaseService.savePurchase(purchase);
    }

    /**
     * 分页查询采购单的url接口
     *
     * @param page
     * @param purchase
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询采购单")
    public Result purchasePageList(Page page, Purchase purchase) {
        Page page1 = purchaseService.queryPurchasePage(page, purchase);
        return Result.ok(page1);
    }

    /**
     * 修改采购单的url接口
     *
     * @param purchase
     * @return
     */
    @PutMapping
    @ApiOperation("修改采购单")
    public Result updatePurchase(@RequestBody Purchase purchase) {
        return purchaseService.updatePurchase(purchase);
    }

    /**
     * 删除采购单的url接口
     *
     * @param buyId
     * @return
     */
    @DeleteMapping("/{buyId}")
    @ApiOperation("删除采购单")
    public Result deletePurchase(@PathVariable Integer buyId) {
        return purchaseService.deletePurchase(buyId);
    }

    /**
     * 添加入库单的url接口/purchase/in-warehouse-record-add
     *
     * @RequestBody Purchase purchase将请求传递的json数据封装到参数Purchase对象;
     * @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token
     * 将请求头Token的值即客户端归还的token赋值给参数变量token;
     */
    @PostMapping("/in-warehouse-record-add")
    public Result addInStore(@RequestBody Purchase purchase, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        //获取当前登录的用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录的用户id -- 创建入库单的用户id
        int createBy = currentUser.getUserId();

        //创建InStore对象封装添加的入库单的信息
        InStore inStore = new InStore();
        inStore.setStoreId(purchase.getStoreId());
        inStore.setProductId(purchase.getProductId());
        inStore.setInNum(purchase.getFactBuyNum());
        inStore.setCreateBy(createBy);

        //执行业务
        return inStoreService.saveInStore(inStore, purchase.getBuyId());
    }

    /**
     * 导出数据
     *
     * @param page
     * @param purchase
     * @return
     */
    @RequestMapping("/exportTable")
    public Result exportTable(Page page, Purchase purchase) {
        page = purchaseService.queryPurchasePage(page, purchase);
        List<?> resultList = page.getResultList();
        //响应
        return Result.ok(resultList);
    }
}
